<template>
  <div class="goods-list">
    <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="getDetail(item.id)">
      <div class="goodshow">
        <img :src="item.img_url" alt>
        <h3>{{item.title}}</h3>
      </div>
      <div class="infos">
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
          <span class="old">￥{{item.market_price}}</span>
        </p>
        <p class="quantity">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div>
    <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      pageIndex: 1, //分页页码，默认为1
      goodslist: [] //商品列表
    };
  },
  created() {
    this.getGoodslist();
  },
  methods: {
    getGoodslist() {
      this.$http
        .get("api/getgoods?pageindex=" + this.pageIndex)
        .then(result => {
          if (result.body.status === 0) {
            if (result.body.message.length === 0) {
              Toast("哥，这回这没了！");
            } else {
              this.goodslist = this.goodslist.concat(result.body.message);
            }
          }
        });
    },
    getMore() {
      this.pageIndex++;
      this.getGoodslist();
    },
    getDetail(id) {
      //使用编程式导航跳转页面
      //1.字符串
      // this.$router.push("/home/goodDetail/"+id);
      // 2.命名的路由
      this.$router.push({ name: "goodDetail", params: { id: id } });
    }
  }
};
</script>
<style lang="scss" scoped>
.goods-list {
  padding: 5px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 273px;
    border: 1px solid #cccccc;
    box-shadow: 0 0 8px #cccccc;
    padding: 4px;
    margin-bottom: 10px;
    .goodshow {
      img {
        width: 100%;
      }
      h3 {
        font-size: 14px;
      }
    }
    .infos {
      background-color: #eeeeee;
      p {
        margin: 0;
        padding: 5px;
      }
      .now {
        color: red;
        font-weight: 600;
      }
      .old {
        margin-left: 10px;
        text-decoration: line-through;
      }
      .quantity {
        font-size: 12px;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
